﻿using OrderSystem.Common;
using OrderSystem.DataAccess;
using OrderSystem.SystemFramework;
using OrderSystem.Templet;
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace OrderSystem
{
    public partial class FormUpdatePayment : FormTemplet
    {

        private long _nPaymentID = -1L;

        private int _nCustomerID = -1;

        private string _sSettleMark = "";

        private long _nPaymentIDParent = -1L;

        private string[] _aOrders = null;

        private DataSet _dsPayment = null;

        private FormUpdatePayment()
        {
            this.InitializeComponent();
        }

        public FormUpdatePayment(long nPaymentID) : this()
        {
            this._nPaymentID = nPaymentID;
            Payments payments = new Payments();
            try
            {
                this._dsPayment = payments.LoadPaymment(nPaymentID);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            payments.Dispose();
            if (this._dsPayment != null)
            {
                if (this._dsPayment.Tables.Count == 3)
                {
                    if (this._dsPayment.Tables[0].Rows.Count > 0)
                    {
                        this.txtPaymentCode.Text = this._dsPayment.Tables[0].Rows[0]["编号"].ToString();
                        this._nCustomerID = Convert.ToInt32(this._dsPayment.Tables[0].Rows[0]["客户编号"]);
                        this.txtCustomerName.Text = this._dsPayment.Tables[0].Rows[0]["客户名称"].ToString();
                        this.dtpPaymentDate.Value = DateTime.Parse(this._dsPayment.Tables[0].Rows[0]["付款日期"].ToString());
                        this.cbPaymentProject.Text = this._dsPayment.Tables[0].Rows[0]["款项"].ToString();
                        this.txtTotalValue.Text = this._dsPayment.Tables[0].Rows[0]["付款金额"].ToString();
                        this.cbWayOfPayment.Text = this._dsPayment.Tables[0].Rows[0]["付款方式"].ToString();
                        this.txtPaymentAddress.Text = this._dsPayment.Tables[0].Rows[0]["付款地址"].ToString();
                        this.txtPaymentRemark.Text = this._dsPayment.Tables[0].Rows[0]["备注"].ToString();
                        base.Width = 400;
                        this._sSettleMark = this._dsPayment.Tables[0].Rows[0]["结算标志"].ToString();
                        if (this._sSettleMark == "S")
                        {
                            this.txtSettleState.Text = "已结算(S)；要改金额数据，必须要取消结算";
                            this.btnCancelSettle.Enabled = true;
                            this.btnSelectCustomer.Enabled = false;
                            this.txtTotalValue.Enabled = false;
                            this.chk是否结算.Enabled = false;
                            this.gbSettlement.Enabled = false;
                            this.panel1.Enabled = false;
                            this.txtSettledDate.Text = this._dsPayment.Tables[0].Rows[0]["结算时间"].ToString();
                            this._nPaymentIDParent = Convert.ToInt64(this._dsPayment.Tables[0].Rows[0]["结算父序号"]);
                            this.btnQueryLastBalance_Click(null, null);
                            this.txtDiscount.Text = this._dsPayment.Tables[0].Rows[0]["优惠"].ToString();
                            this.txt结算余额.Text = this._dsPayment.Tables[0].Rows[0]["结算余额"].ToString();
                            if (this._dsPayment.Tables[1] != null)
                            {
                                this._aOrders = new string[this._dsPayment.Tables[1].Rows.Count];
                                decimal d = 0m;
                                for (int i = 0; i < this._dsPayment.Tables[1].Rows.Count; i++)
                                {
                                    d += Convert.ToDecimal(this._dsPayment.Tables[1].Rows[i][1]);
                                    this._aOrders[i] = this._dsPayment.Tables[1].Rows[i][0].ToString();
                                }
                                this.txtOrderAmount.Text = d.ToString("F2");
                            }
                            if (this._dsPayment.Tables[2] != null)
                            {
                                base.Width = 1156;
                                this.dgPayment.DataSource = this._dsPayment.Tables[2];
                                this.dgPayment.AutoResizeColumns();
                                decimal d2 = 0m;
                                for (int i = 0; i < this._dsPayment.Tables[2].Rows.Count; i++)
                                {
                                    d2 += Convert.ToDecimal(this._dsPayment.Tables[2].Rows[i][6]);
                                }
                                this.txtSettleAmount.Text = d2.ToString("F2");
                            }
                        }
                        else if (this._sSettleMark == "P")
                        {
                            this.txtSettleState.Text = "被结算(P)；要改金额数据，必须要把结算此单的收款单取消结算";
                            this.btnCancelSettle.Enabled = false;
                            this.btnSelectCustomer.Enabled = false;
                            this.txtTotalValue.Enabled = false;
                            this.chk是否结算.Enabled = false;
                            this.gbSettlement.Enabled = false;
                            this.panel1.Enabled = false;
                        }
                        else if (this._sSettleMark == "N")
                        {
                            this.txtSettleState.Text = "未结算(N)；";
                            this.btnCancelSettle.Enabled = false;
                            this.txtTotalValue.Enabled = true;
                            this.chk是否结算.Enabled = true;
                            this.gbSettlement.Enabled = false;
                            this.panel1.Enabled = false;
                        }
                        else
                        {
                            this.txtSettleState.Text = "作废(C)；";
                            this.btnCancelSettle.Enabled = false;
                            this.txtTotalValue.Enabled = false;
                            this.chk是否结算.Enabled = false;
                            this.gbSettlement.Enabled = false;
                            this.panel1.Enabled = false;
                            this.ckCancelled.Checked = true;
                        }
                        this.btnOk.Enabled = true;
                    }
                    else
                    {
                        MessageBox.Show("可能没有数据信息！", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
                else
                {
                    MessageBox.Show("可能没有数据信息！", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
        }

        private void FormQueryOrders_Load(object sender, EventArgs e)
        {
            this.dtpPaymentDateStart.Enabled = false;
            this.dtpPaymentDateEnd.Enabled = false;
            this.dgPayment.AutoResizeColumns();
        }

        private void btnSelectCustomer_Click(object sender, EventArgs e)
        {
            CustomerData customerData = FormSelectCustomer.ShowBox();
            if (customerData != null)
            {
                this._nCustomerID = customerData.CustomerID;
                this.txtCustomerName.Text = customerData.CompanyName + "(" + customerData.Linkman + ")";
                this.txtCustomerName.Focus();
                this.btnQueryLastBalance_Click(null, null);
                this.txtDiscount.Text = "0";
                this.txt结算余额.Text = "0.00";
                this.txtOrderAmount.Text = "0.00";
                this._aOrders = null;
                this.dgPayment.DataSource = null;
                this.txtSettleAmount.Text = "0.00";
            }
        }

        private void btnCancelSettle_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("你确定要取消此次结算吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.Cancel)
            {
                Payments payments = new Payments();
                bool flag = false;
                try
                {
                    flag = payments.CancelSettle(this._nPaymentID);
                }
                catch (SqlException ex)
                {
                    if (ex.Number >= 50000)
                    {
                        MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    else
                    {
                        MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
                catch (Exception ex2)
                {
                    MessageBox.Show(ex2.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                payments.Dispose();
                if (flag)
                {
                    MessageBox.Show("设置成功！", "Success", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    this._sSettleMark = "N";
                    this.txtSettleState.Text = "未结算(N)；";
                    this._aOrders = null;
                    this.btnSelectCustomer.Enabled = true;
                    this.txtTotalValue.Enabled = true;
                    this.chk是否结算.Enabled = true;
                    this.gbSettlement.Enabled = false;
                    this.panel1.Enabled = false;
                    this.txtLastSettleBalance.Text = "0.00";
                    this.txtDiscount.Text = "0.00";
                    this.txt结算余额.Text = "0.00";
                    this.txtOrderAmount.Text = "0.00";
                    this.txtSettleAmount.Text = "0.00";
                    this.dgPayment.DataSource = null;
                }
            }
        }

        private void ckCancelled_Click(object sender, EventArgs e)
        {
            if (this.chk是否结算.Checked)
            {
                MessageBox.Show("此付款单正在作结算处理...，不能作废！", "Success", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else if (this.ckCancelled.Checked)
            {
                Payments payments = new Payments();
                bool flag = false;
                try
                {
                    flag = payments.Activate(this._nPaymentID);
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message + "\r\n\r\n", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                catch (Exception ex2)
                {
                    MessageBox.Show(ex2.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                payments.Dispose();
                if (flag)
                {
                    this.ckCancelled.Checked = false;
                    this.txtSettleState.Text = "未结算(N)；";
                }
            }
            else
            {
                Payments payments = new Payments();
                bool flag = false;
                try
                {
                    flag = payments.Cancel(this._nPaymentID);
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message + "\r\n\r\n", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                catch (Exception ex2)
                {
                    MessageBox.Show(ex2.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                payments.Dispose();
                if (flag)
                {
                    this.ckCancelled.Checked = true;
                    this.txtSettleState.Text = "作废(C)；";
                }
            }
        }

        private void chk是否结算_Click(object sender, EventArgs e)
        {
            if (this.ckCancelled.Checked)
            {
                MessageBox.Show("此付款单已经作废，不能结算！", "Success", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else if (!this.chk是否结算.Checked)
            {
                this.btnQueryLastBalance_Click(null, null);
                this.gbSettlement.Enabled = true;
                this.panel1.Enabled = true;
                base.Width = 1156;
                this.chk是否结算.Checked = true;
            }
            else
            {
                this.gbSettlement.Enabled = false;
                this.panel1.Enabled = false;
                base.Width = 400;
                this.chk是否结算.Checked = false;
            }
        }

        private void btnQueryOrderAmount_Click(object sender, EventArgs e)
        {
            if (this.txtCustomerName.Text.Trim() == "")
            {
                MessageBox.Show("客户名称必须选择！！！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                OrdersAmountData ordersAmountData = FormSelectOrders.ShowBox(this._nCustomerID, this.txtCustomerName.Text);
                if (ordersAmountData != null)
                {
                    this._aOrders = ordersAmountData.Orders;
                    this.txtOrderAmount.Text = ordersAmountData.Amount.ToString();
                    this.btnOk.Focus();
                }
            }
        }

        private void chkPaymentDateUsed_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chkPaymentDateUsed.Checked)
            {
                this.dtpPaymentDateStart.Enabled = true;
                this.dtpPaymentDateEnd.Enabled = true;
            }
            else
            {
                this.dtpPaymentDateStart.Enabled = false;
                this.dtpPaymentDateEnd.Enabled = false;
            }
        }

        private PaymentCondition GetConditionForQueryPayment()
        {
            PaymentCondition paymentCondition = new PaymentCondition();
            PaymentCondition result;
            if (this.txtCustomerName.Text.Trim() == "")
            {
                MessageBox.Show("客户名称必须选择！！！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                result = null;
            }
            else
            {
                if (this.txtCustomerName.Text.Trim() != "")
                {
                    if (this.txtCustomerName.Text.Trim() == "" || this._nCustomerID == -1)
                    {
                        MessageBox.Show("请选择客户", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        this.txtCustomerName.Focus();
                        result = null;
                        return result;
                    }
                    paymentCondition.CustomerID = this._nCustomerID;
                }
                if (this.chkPaymentDateUsed.Checked)
                {
                    int num = DateTime.Parse(this.dtpPaymentDateStart.Value.ToShortDateString() + " 00:00:00").CompareTo(DateTime.Parse(this.dtpPaymentDateEnd.Value.ToShortDateString() + " 23:59:59"));
                    if (num > 0)
                    {
                        MessageBox.Show("订购日期：后面的日期要大于或等于前面的日期", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        this.dtpPaymentDateStart.Focus();
                        result = null;
                        return result;
                    }
                    paymentCondition.PaymentDateStart = this.dtpPaymentDateStart.Value.ToShortDateString() + " 00:00:00";
                    paymentCondition.PaymentDateEnd = this.dtpPaymentDateEnd.Value.ToShortDateString() + " 23:59:59";
                }
                paymentCondition.SettleMark = "'N'";
                result = paymentCondition;
            }
            return result;
        }

        private void btnNosettlementPayment_Click(object sender, EventArgs e)
        {
            PaymentCondition conditionForQueryPayment = this.GetConditionForQueryPayment();
            if (conditionForQueryPayment != null)
            {
                DataTable dataTable = null;
                Payments payments = new Payments();
                try
                {
                    dataTable = payments.GetPaymentsByCondition(conditionForQueryPayment);
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！\r\n\r\n信息可能没有读出来", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
                catch (Exception ex2)
                {
                    MessageBox.Show(ex2.Message + "\r\n\r\n信息可能没有读出来", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                payments.Dispose();
                if (dataTable != null)
                {
                    dataTable.Columns.Add(new DataColumn("是否结算", typeof(bool)));
                    if (dataTable.Rows.Count > 0)
                    {
                        for (int i = 0; i < dataTable.Rows.Count; i++)
                        {
                            if (dataTable.Rows[i]["编号"].ToString() == this.txtPaymentCode.Text || Convert.ToInt64(dataTable.Rows[i]["序号"]) == this._nPaymentID)
                            {
                                dataTable.Rows[i].Delete();
                                dataTable.AcceptChanges();
                                break;
                            }
                        }
                        this.dgPayment.DataSource = null;
                        this.dgPayment.DataSource = dataTable;
                        this.dgPayment.AutoResizeColumns();
                        this.dgPayment.Columns["序号"].Visible = false;
                        this.dgPayment.Columns["是否结算"].DisplayIndex = 0;
                        for (int i = 0; i < this.dgPayment.Rows.Count; i++)
                        {
                            this.dgPayment.Rows[i].Cells["是否结算"].Value = false;
                        }
                    }
                    else
                    {
                        MessageBox.Show("根据你的信息，在系统中没有找到你要得信息\r\n\r\n信息可能没有读出来", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        this.dgPayment.DataSource = null;
                    }
                }
                this.txtSettleAmount.Text = "0.00";
            }
        }

        private void dgPayment_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.RowIndex < this.dgPayment.Rows.Count && e.ColumnIndex >= 0 && e.ColumnIndex < this.dgPayment.Columns.Count && this.dgPayment != null && this.dgPayment.Rows.Count > 0 && this.dgPayment.Columns[e.ColumnIndex].Name == "是否结算")
            {
                if ((bool)this.dgPayment.Rows[e.RowIndex].Cells[e.ColumnIndex].Value)
                {
                    this.dgPayment.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = false;
                }
                else
                {
                    this.dgPayment.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = true;
                }
                decimal d = 0m;
                for (int i = 0; i < this.dgPayment.Rows.Count; i++)
                {
                    if ((bool)this.dgPayment.Rows[i].Cells["是否结算"].Value)
                    {
                        d += Convert.ToDecimal(this.dgPayment.Rows[i].Cells["金额"].Value);
                    }
                }
                this.txtSettleAmount.Text = d.ToString("F2");
            }
        }

        private void txtTotalValue_TextChanged(object sender, EventArgs e)
        {
            if (Regex.IsMatch(this.txtTotalValue.Text.Trim(), "^[-]{0,1}(([0-9]{1,})|([0-9]{1,}\\.[0-9]{1,2}))$"))
            {
                if (Regex.IsMatch(this.txtDiscount.Text.Trim(), "^[-]{0,1}(([0-9]{1,})|([0-9]{1,}\\.[0-9]{1,2}))$"))
                {
                    if (Regex.IsMatch(this.txtLastSettleBalance.Text.Trim(), "^[-]{0,1}(([0-9]{1,})|([0-9]{1,}\\.[0-9]{1,2}))$"))
                    {
                        this.txt结算余额.Text = (Convert.ToDecimal(this.txtLastSettleBalance.Text) + Convert.ToDecimal(this.txtSettleAmount.Text) + Convert.ToDecimal(this.txtTotalValue.Text) - Convert.ToDecimal(this.txtOrderAmount.Text) + Convert.ToDecimal(this.txtDiscount.Text)).ToString();
                    }
                }
            }
        }

        private void txtSettleAmount_TextChanged(object sender, EventArgs e)
        {
            if (Regex.IsMatch(this.txtTotalValue.Text.Trim(), "^[-]{0,1}(([0-9]{1,})|([0-9]{1,}\\.[0-9]{1,2}))$"))
            {
                if (Regex.IsMatch(this.txtDiscount.Text.Trim(), "^[-]{0,1}(([0-9]{1,})|([0-9]{1,}\\.[0-9]{1,2}))$"))
                {
                    if (Regex.IsMatch(this.txtLastSettleBalance.Text.Trim(), "^[-]{0,1}(([0-9]{1,})|([0-9]{1,}\\.[0-9]{1,2}))$"))
                    {
                        this.txt结算余额.Text = (Convert.ToDecimal(this.txtLastSettleBalance.Text) + Convert.ToDecimal(this.txtSettleAmount.Text) + Convert.ToDecimal(this.txtTotalValue.Text) - Convert.ToDecimal(this.txtOrderAmount.Text) + Convert.ToDecimal(this.txtDiscount.Text)).ToString();
                    }
                }
            }
        }

        private void txtOrderAmount_TextChanged(object sender, EventArgs e)
        {
            if (Regex.IsMatch(this.txtTotalValue.Text.Trim(), "^[-]{0,1}(([0-9]{1,})|([0-9]{1,}\\.[0-9]{1,2}))$"))
            {
                if (Regex.IsMatch(this.txtDiscount.Text.Trim(), "^[-]{0,1}(([0-9]{1,})|([0-9]{1,}\\.[0-9]{1,2}))$"))
                {
                    if (Regex.IsMatch(this.txtLastSettleBalance.Text.Trim(), "^[-]{0,1}(([0-9]{1,})|([0-9]{1,}\\.[0-9]{1,2}))$"))
                    {
                        this.txt结算余额.Text = (Convert.ToDecimal(this.txtLastSettleBalance.Text) + Convert.ToDecimal(this.txtSettleAmount.Text) + Convert.ToDecimal(this.txtTotalValue.Text) - Convert.ToDecimal(this.txtOrderAmount.Text) + Convert.ToDecimal(this.txtDiscount.Text)).ToString();
                    }
                }
            }
        }

        private void txtDiscount_TextChanged(object sender, EventArgs e)
        {
            if (Regex.IsMatch(this.txtTotalValue.Text.Trim(), "^[-]{0,1}(([0-9]{1,})|([0-9]{1,}\\.[0-9]{1,2}))$"))
            {
                if (Regex.IsMatch(this.txtDiscount.Text.Trim(), "^[-]{0,1}(([0-9]{1,})|([0-9]{1,}\\.[0-9]{1,2}))$"))
                {
                    if (Regex.IsMatch(this.txtLastSettleBalance.Text.Trim(), "^[-]{0,1}(([0-9]{1,})|([0-9]{1,}\\.[0-9]{1,2}))$"))
                    {
                        this.txt结算余额.Text = (Convert.ToDecimal(this.txtLastSettleBalance.Text) + Convert.ToDecimal(this.txtSettleAmount.Text) + Convert.ToDecimal(this.txtTotalValue.Text) - Convert.ToDecimal(this.txtOrderAmount.Text) + Convert.ToDecimal(this.txtDiscount.Text)).ToString();
                    }
                }
            }
        }

        private void txtLastSettleBalance_TextChanged(object sender, EventArgs e)
        {
            if (Regex.IsMatch(this.txtTotalValue.Text.Trim(), "^[-]{0,1}(([0-9]{1,})|([0-9]{1,}\\.[0-9]{1,2}))$"))
            {
                if (Regex.IsMatch(this.txtDiscount.Text.Trim(), "^[-]{0,1}(([0-9]{1,})|([0-9]{1,}\\.[0-9]{1,2}))$"))
                {
                    if (Regex.IsMatch(this.txtLastSettleBalance.Text.Trim(), "^[-]{0,1}(([0-9]{1,})|([0-9]{1,}\\.[0-9]{1,2}))$"))
                    {
                        this.txt结算余额.Text = (Convert.ToDecimal(this.txtLastSettleBalance.Text) + Convert.ToDecimal(this.txtSettleAmount.Text) + Convert.ToDecimal(this.txtTotalValue.Text) - Convert.ToDecimal(this.txtOrderAmount.Text) + Convert.ToDecimal(this.txtDiscount.Text)).ToString();
                    }
                }
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            this.btnQueryLastBalance_Click(null, null);
            this.txtTotalValue_TextChanged(null, null);
            long[] aPaymentID = null;
            if (this.chk是否结算.Checked)
            {
                ArrayList arrayList = new ArrayList();
                for (int i = 0; i < this.dgPayment.Rows.Count; i++)
                {
                    if ((bool)this.dgPayment.Rows[i].Cells["是否结算"].Value)
                    {
                        arrayList.Add(this.dgPayment.Rows[i].Cells["序号"].Value);
                    }
                }
                aPaymentID = (arrayList.ToArray(typeof(long)) as long[]);
            }
            if (this.txtCustomerName.Text.Trim() == "" || this._nCustomerID < 0)
            {
                MessageBox.Show("请选择客户", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.btnSelectCustomer.Focus();
            }
            else if (this.txtCustomerName.Focused)
            {
                this.dtpPaymentDate.Focus();
            }
            else if (this.dtpPaymentDate.Focused)
            {
                this.cbPaymentProject.Focus();
            }
            else if (this.cbPaymentProject.Text.Trim().Length > 32 || this.cbPaymentProject.Text.Trim().Length < 1)
            {
                MessageBox.Show("款项 格式不正确，字符不能为空 且 小于等于32个字符", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else if (this.cbPaymentProject.Focused)
            {
                this.txtTotalValue.Focus();
            }
            else if (!Regex.IsMatch(this.txtTotalValue.Text.Trim(), "^[-]{0,1}(([0-9]{1,})|([0-9]{1,}\\.[0-9]{1,4}))$"))
            {
                MessageBox.Show("金额 格式不正确，此处应该为不为零的整数，如有小数，最大为4位小数", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else if (this.txtTotalValue.Focused)
            {
                this.cbWayOfPayment.Focus();
            }
            else if (this.cbWayOfPayment.Text.Trim().Length > 16 || this.cbWayOfPayment.Text.Trim().Length < 1)
            {
                MessageBox.Show("付款方式 格式不正确，字符不能为空 且 小于等于16个字符", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else if (this.cbWayOfPayment.Focused)
            {
                this.txtPaymentAddress.Focus();
            }
            else if (this.txtPaymentAddress.Text.Trim().Length > 64)
            {
                MessageBox.Show("付款地址 格式不正确，应小于等于64个字符", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else if (this.txtPaymentAddress.Focused)
            {
                this.txtPaymentRemark.Focus();
            }
            else if (this.txtPaymentRemark.Text.Trim().Length > 64)
            {
                MessageBox.Show("备注 格式不正确，应小于等于64个字符", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else if (this.txtPaymentRemark.Focused)
            {
                this.chk是否结算.Focus();
            }
            else
            {
                if (this.chk是否结算.Focused)
                {
                    if (this.chk是否结算.Checked)
                    {
                        this.txtDiscount.Focus();
                        return;
                    }
                }
                if (this.chk是否结算.Checked)
                {
                    if (!Regex.IsMatch(this.txtDiscount.Text.Trim(), "^[-]{0,1}(([0-9]{1,})|([0-9]{1,}\\.[0-9]{1,4}))$"))
                    {
                        MessageBox.Show("优惠 格式不正确，此处应该为不为零的整数，如有小数，最大为4位小数", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                    if (this.txtDiscount.Focused)
                    {
                        this.btnQueryOrderAmount.Focus();
                        return;
                    }
                }
                if (MessageBox.Show("请确定输入的数据准确无误......", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.Cancel)
                {
                    Payments payments = new Payments();
                    bool flag = false;
                    try
                    {
                        if (this.chk是否结算.Checked)
                        {
                            flag = payments.UpdatePayment(this._nPaymentID, "", this._nCustomerID, this.dtpPaymentDate.Value, this.cbPaymentProject.Text.Trim(), Convert.ToDouble(this.txtTotalValue.Text.Trim()), this.cbWayOfPayment.Text.Trim(), this.txtPaymentAddress.Text.Trim(), this.txtPaymentRemark.Text.Trim(), this.chk是否结算.Checked, aPaymentID, Convert.ToDecimal(this.txtDiscount.Text.Trim()), Convert.ToDecimal(this.txt结算余额.Text.Trim()), this._aOrders, LoginInfo.OperatorID);
                        }
                        else
                        {
                            flag = payments.UpdatePayment(this._nPaymentID, "", this._nCustomerID, this.dtpPaymentDate.Value, this.cbPaymentProject.Text.Trim(), Convert.ToDouble(this.txtTotalValue.Text.Trim()), this.cbWayOfPayment.Text.Trim(), this.txtPaymentAddress.Text.Trim(), this.txtPaymentRemark.Text.Trim(), this.chk是否结算.Checked, null, 0m, 0m, null, LoginInfo.OperatorID);
                        }
                    }
                    catch (SqlException ex)
                    {
                        MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    }
                    catch (Exception ex2)
                    {
                        MessageBox.Show(ex2.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    payments.Dispose();
                    if (flag)
                    {
                        MessageBox.Show("修改成功！", "Success", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                        base.Close();
                    }
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btnQueryLastBalance_Click(object sender, EventArgs e)
        {
            if (this.txtCustomerName.Text.Trim() == "")
            {
                MessageBox.Show("客户名称必须选择！！！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                Payments payments = new Payments();
                try
                {
                    decimal lastSettleBalance = payments.GetLastSettleBalance(this._nPaymentIDParent, this._nCustomerID, "");
                    this.txtLastSettleBalance.Text = lastSettleBalance.ToString();
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                catch (Exception ex2)
                {
                    MessageBox.Show(ex2.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                payments.Dispose();
            }
        }

        private void btnSettleDOrder_Click(object sender, EventArgs e)
        {
            string text = "※ 此次结算包含的订单如下：\r\n";
            for (int i = 0; i < this._dsPayment.Tables[1].Rows.Count; i++)
            {
                text = text + "\r\n" + CommonMethod.PadSpaceOnRight("订单号----------", 16) + CommonMethod.PadSpaceOnRight("金额------", 10);
                text = text + "\r\n" + CommonMethod.PadSpaceOnRight(this._dsPayment.Tables[1].Rows[i][0].ToString(), 16) + CommonMethod.PadSpaceOnRight(this._dsPayment.Tables[1].Rows[i][1].ToString(), 10);
            }
            if (ContentBox.ShowBox("订单类型_价格总结", text, "是否保存这些数据 ?") == DialogResult.Yes)
            {
            }
        }
    }
}
